﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameNesSL.Froms
{
    using System.Threading;
    using System.Windows.Media.Imaging;

    public enum MessageBoxIcon
    {
        Waring,
        Error,
        Information
    }
    public enum MessageBoxButtons
    {
        YesNo,
        OkCancel,
        Ok
    }

    public partial class MessageBox : ChildWindow
    {


        public MessageBox(MessageBoxIcon mbIcon, MessageBoxButtons mbButton, string captinon, string text)
        {
            Height = 240;
            
            InitializeComponent();
            this.HasCloseButton = false;
            Title = captinon;
            switch (mbButton)
            {
                case MessageBoxButtons.Ok:
                    CancelButton.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case MessageBoxButtons.YesNo:
                    CancelButton.Content = "No";
                    OKButton.Content = "Yes";
                    break;
            }
            switch (mbIcon)
            {
                case MessageBoxIcon.Error:
                    errorIcon.Source = new BitmapImage(new Uri("../images/dialogicons/dialog-error.png",UriKind.Relative));
                    break;
                case MessageBoxIcon.Waring:
                    errorIcon.Source = new BitmapImage(new Uri("../images/dialogicons/dialog-warning.png", UriKind.Relative));
                    break;
                case MessageBoxIcon.Information:
                    errorIcon.Source = new BitmapImage(new Uri("../images/dialogicons/dialog-information.png", UriKind.Relative));
                    break;
            }

            description.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
            string[] lines = text.Split('\r', '\n');
            foreach (var line in lines)
            {
                if (!string.IsNullOrEmpty(line))
                {
                    description.Inlines.Add(line);
                }
                else
                {
                    description.Inlines.Add(new LineBreak());
                }
            }
        }

        public delegate void ModalDelegate(bool result);

        private ModalDelegate modalFunc = null;

        public void ShowModal(ModalDelegate modalFunc)
        {
            this.modalFunc = modalFunc;
            Show();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            if (modalFunc!=null)
            {
                modalFunc(true);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            if (modalFunc != null)
            {
                modalFunc(false);
            }
        }
    }
}

